<?php
class Areas extends AreasAppModel {
	var $useTable = 'sys_areas';
	public $actsAs = array('Pagination');
	var $tree = array();
	var $recursive = -1;
	
	public $validate = array(
		'pid' => array(
			'rule' => 'notEmpty',
			'message'  => '请输入父级ID'
		),
		'name' => array(
			'rule' => 'notEmpty',
			'message'  => '请输入地区名称'
		)
	);

	/**
	 * 分页条件
	 * @Date 2013-12-20
	 * @return Array
	 */
	function pageParam($action){
		return array(
				"colNames"=>array(
						'ID','名称','排序','启用','操作'
				),
				"colModel"=>array(
						array("name"=>'Areas.id',"width"=>'50',"sortable"=>false,"align"=>"center"),
						array("name"=>'Areas.name',"width"=>"300","sortable"=>false),
						array("name"=>'Areas.sort',"width"=>"100","sortable"=>false,"align"=>"center"),
						array("name"=>'Areas.enable',"width"=>"50","sortable"=>false,"align"=>"center","formatter"=>"checkbox"),
						array("name"=>'act',"width"=>"200","align"=>"center","sortable"=>false)
				),
				"ExpandColumn"=>"Areas.name",
				"ajax"=>true,
				"action"=>$action
		);
	}

	/**
	 * 获取地区
	 * ext cloum
	 * level/parent/isLeaf/expanded/loaded/icon
	 * @Date 2013-12-30
	 * @param $parentId 上级id
	 * @param $level 层级
	 * @return 
	 */
	function ajaxTreeCategory($parentId,$level){
	
		$t = $this->findCategory($parentId,'all');
		$leafTree = array();
		$row = array();
	
		foreach($t as $k => $v){
			array_push($leafTree,$v['Areas']['id']);
			array_push($leafTree,$v['Areas']['name']);
			array_push($leafTree,$v['Areas']['sort']);
			array_push($leafTree,$v['Areas']['enable']);
			array_push($leafTree,"");
				
			//level
			array_push($leafTree,$level);
			
			//parent
			array_push($leafTree,$parentId);
			
			//isLeaf
			$hasSon = $this->findSonNodel($v['Areas']['id'],'all');
			if($hasSon){
				array_push($leafTree,false);
			}else{
				array_push($leafTree,true);
			}
			
			//expanded
			if($v['Areas']['is_open']==1){
				$treeLoaded = true;
				array_push($leafTree,true);
			}else{
				$treeLoaded = false;
				array_push($leafTree,false);
			}
			
			//loaded
			array_push($leafTree,$treeLoaded);
				
			$row['id'] = $v['Areas']['id'];
			$row['cell'] = $leafTree;
			
			$leafTree = array();
			
			if(sizeof($row)>0){
				array_push($this->tree,$row);
			}
				
			if($v['Areas']['is_open']==1){
				$this->ajaxTreeCategory($v['Areas']['id'],$level+1);
			}
		}
		return $this->tree;
	}
	
	/**
	 * 获取地区分类
	 * @author lj@gootop.net  
	 * @Date 2013-12-20
	 * @param
	 * @return 
	 */
	function findCategory($id = 0,$type = 'enable') {
		$conditions = array('conditions' => array(
				'Areas.parent_id'=>$id
		),
				'order' => array('Areas.sort ASC')
		) ;
		if($type == 'enable'){
			$conditions['conditions']['Areas.enable'] = 1;
		}
		return $this->find('all',$conditions);
	}
	
	/**
	 * 检测子节点
	 * @author zmx@gootop.net
	 * @Date 2013-12-20
	 * @param
	 * @return
	 */
	public function findSonNodel($parent_id = 0,$type = 'enable'){
		$conditions = array(
				'conditions'=>array(
						'parent_id'=>$parent_id
				)
		);
		if($type == 'enable'){
			$conditions['conditions']['Areas.enable'] = 1;
		}
		return $this->find('first',$conditions);
		
	}
	
}
?>